<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <label class="el-form-item-label">总公司名称</label>
        <!-- 搜索 -->
        <el-select
          v-model="query.companyId"
          clearable
          filterable
          placeholder="请选择"
          style="width: 185px"
          class="filter-item"
          @change="crud.toQuery"
        >
          <el-option
            v-for="item in dict.company_id"
            :key="item.id"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <label class="el-form-item-label">分公司名称</label>
        <el-input
          v-model="query.dcompanyName"
          clearable
          placeholder="分公司名称"
          style="width: 185px"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog
        :close-on-click-modal="false"
        :before-close="crud.cancelCU"
        :visible.sync="crud.status.cu > 0"
        :title="crud.status.title"
        width="600px"
      >
        <el-form
          ref="form"
          :model="form"
          :rules="rules"
          size="small"
          label-width="120px"
        >
          <el-form-item label="总公司名称">
            <el-select v-model="form.companyId" filterable placeholder="请选择">
              <el-option
                v-for="item in dict.company_id"
                :key="item.id"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="分公司名称">
            <el-input v-model="form.dcompanyName" style="width: 198px" />
          </el-form-item>

          <el-form-item label="分公司属地">
            <!-- <el-input v-model="" style="width: 370px" /> -->
            <el-cascader
              :options="options"
              v-model="form.dcompanyCode"
              @change="handleChange"
              clearable
            ></el-cascader>
          </el-form-item>
          <el-form-item label="分公司详细地址">
            <el-input v-model="form.dcompanyAddress" style="width: 50%" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button
            :loading="crud.status.cu === 2"
            type="primary"
            @click="crud.submitCU"
            >确认</el-button
          >
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table
        ref="table"
        v-loading="crud.loading"
        :data="crud.data"
        size="small"
        style="width: 100%"
        @selection-change="crud.selectionChangeHandler"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column prop="companyId" label="总公司名称">
          <template slot-scope="scope">
            {{ dict.label.company_id[scope.row.companyId] }}
          </template>
        </el-table-column>
        <el-table-column prop="dcompanyName" label="分公司名称" />
        <el-table-column prop="dcompanyAddress" label="分公司地址" />
        <el-table-column prop="dcompanyCode" label="分公司属地编码" />
        <el-table-column
          v-if="
            checkPer(['admin', 'dependentCompany:edit', 'dependentCompany:del'])
          "
          label="操作"
          width="150px"
          align="center"
        >
          <template slot-scope="scope">
            <udOperation :data="scope.row" :permission="permission" />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudDependentCompany from "@/api/dcompany/dependentCompany.js";
import CRUD, { presenter, header, form, crud } from "@crud/crud";
import rrOperation from "@crud/RR.operation";
import crudOperation from "@crud/CRUD.operation";
import udOperation from "@crud/UD.operation";
import pagination from "@crud/Pagination";

const defaultForm = {
  dcompanyId: null,
  companyId: null,
  dcompanyName: null,
  dcompanyAddress: null,
  dcompanyCode: null,
};
export default {
  name: "DependentCompany",
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ["company_id"],
  cruds() {
    return CRUD({
      title: "分公司接口管理",
      url: "api/dependentCompany",
      idField: "dcompanyId",
      sort: "dcompanyId,desc",
      crudMethod: { ...crudDependentCompany },
    });
  },
  data() {
    return {
      options: [],
      permission: {
        add: ["admin", "dependentCompany:add"],
        edit: ["admin", "dependentCompany:edit"],
        del: ["admin", "dependentCompany:del"],
      },
      rules: {
        dcompanyId: [
          { required: true, message: "分公司主键id不能为空", trigger: "blur" },
        ],
      },
      queryTypeOptions: [
        { key: "companyId", display_name: "关联总公司主键id" },
        { key: "dcompanyName", display_name: "分公司名称" },
      ],
    };
  },
  methods: {
    handleChange(value) {
      console.log(value[2]);
      this.form.dcompanyCode = value[2];
    },
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      crudDependentCompany.get().then((res) => {
        this.options = res;
      });
      return true;
    },
  },
};
</script>

<style scoped>
</style>
